<template>
	<view class="page_pingjia4">
		<view class="kuang">
			<view class="biaoti" v-for="(item,index) in por">{{item.title}}</view>
			<view class="xx">
				<start :count="count" v-model="score" size="35" activeColor="#fe3030"></start>
			</view>
			<!-- <view class="xkuang">
				<view style="display: flex;" v-for="(item,index) in rate">
					<view class="ziti">{{item.name}}</view>
					<start :count="item.count" style="margin-top: 15px; margin-left: 50px;" size="35"
						activeIcon="heart-fill" inactiveIcon="heart" activeColor="#ffaa00" v-model="item.value1">
					</start>
				</view>
			</view> -->
			<view style="margin: 20rpx 0;">
				<u--textarea style="margin-top: 20px;" background-color="#6d6d6d" v-model="comment" placeholder="请输入内容">
				</u--textarea>
			</view>
			<u-upload :fileList="fileList1" style="margin-top: 10px;" @afterRead="afterRead" @delete="deletePic"
				name="1" multiple :maxCount="3" action='链接地址'></u-upload>
			<view style="color: #ccc;">
				最多上传3张图片
			</view>
			<!-- <view class="niming">
				<u-radio-group v-model="radiovalue1" placement="column" @change="groupChange">
					<u-radio :customStyle="{marginBottom: '8px'}" v-for="(item, index) in hidden" :key="index"
						:label="item.name" :name="item.name">
					</u-radio>
				</u-radio-group>
			</view> -->
		</view>
		<view class="anniu" v-for="(item,index) in button">
			<!-- <u-button type="warning">{{item.name}}</u-button> -->
			<view class="login-btn" @click="submit">
				{{item.name}}
			</view>
		</view>
	</view>
</template>

<script>
	import start from '../../components/uni-rate.vue'
	export default {
		components: {
			start
		},
		data() {
			return {
				count: "5",
				score: '',
				comment: '',
				radiovalue1: '',
				fileList1: [],
				por: [{
					title: '商品评分',
				}],
				button: [{
					name: '提交'
				}],

				hidden: [{
					name: '匿名评论',
					disabled: false
				}],
				imagesList: []
			}
		},
		onLoad(option) {
			console.log(option)
			this.order_id = option.id
		},
		methods: {
			groupChange(n) {
				console.log('groupChange', n);
			},
			radioChange(n) {
				console.log('radioChange', n);
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 提交评论
			submit() {
				if(!this.score){
					this.$u.toast('您尚未评分')
					return
				}
				if(!this.comment){
					this.$u.toast('您尚未填写评价内容')
					return
				}
				uni.$u.http.post('/shop/addcomment', {
					order_id: this.order_id,
					score: this.score,
					comment: this.comment,
					images: this.fileList1
				}).then(res => {
					this.$u.toast(res.msg)
					if (res.code === 1) {
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						})
					}
				})
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].thumb)
					console.log(result)
					this.imagesList.push(result.data.fullurl)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, 
						result.data.fullurl
					)
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: uni.$u.http.config.baseURL + '/common/upload',
						filePath: url,
						name: 'file',
						header: {
							token: uni.getStorageSync('token')
						},
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(JSON.parse(res.data))
							}, 1000)
						}
					});
				})
			}
		}
	}
</script>

<style lang="scss">
	.page_pingjia4 {
		width: 100vw;
		height: 80vh;
		background-color: #ffffff;

		.anniu {
			width: 90vw;
			margin-left: 20px;

			.login-btn {
				margin: 0 auto;
				width: 572rpx;
				height: 84rpx;
				background: #D8D8D8 linear-gradient(136deg, #FFECCA 0%, #E7BC70 100%);
				border-radius: 42rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #333333;
				text-align: center;
				line-height: 84rpx;
				// margin-top: 86rpx;
			}
		}

		.niming {
			margin-top: 20px;
		}

		.ziti {
			color: #909090;
			margin-top: 23px;
		}

		.xkuang {
			margin-top: 20px;
			height: 18vh;
			background-color: #FAFAFA;
			padding-left: 30px;
		}

		.xx {
			margin-left: 20rpx;
			padding-top: 10px;
		}

		.biaoti {
			// width: 80%;
			margin-left: 20rpx;
			// text-align: center;
			margin-top: 20px;
		}

		.kuang {
			// width: 90vw;
			height: 50vh;
			background-color: #ffffff;
			padding: 10rpx 30rpx;
		}
	}
</style>
